import { useReducer, useState } from 'react';
import _ from 'lodash';
const App = (props, queue = false) => {
  //记录数据用来回到初始化状态
  let __props__ = _.cloneDeep(props);

  //历史数据的队列 用来实现撤回
  let historicalData = [];
  const [index, setIndex] = useState(0);
  const [store, dispatch] = useReducer((state, action) => {
    try {
      return { ...state, ...action };
    } catch (error) {
      console.error(error);
      return { ...state };
    }
  }, props);
  const _dispatch = (action, fun) => {
    if (historicalData.length <= 10) {
      historicalData.push(_.cloneDeep(action));
    } else {
      historicalData.shift();
      historicalData.push(_.cloneDeep(action));
    }
    setIndex(historicalData.length);
    dispatch(action);
    fun && fun({ ...store, ...action }, store);
  };
  return [
    store,
    _dispatch,
    {
      clearState: () => {
        dispatch(__props__);
      },
      lastState: () => {},
      nextState: () => {},
    },
  ];
};
export default App;
